package YuanFuDao2;

/**
 * 二维数组回行打印
 * // 方法1：递归
 */
public class Main5 {

    private static int minN = -1;
    private static int minM = -1;
    private static int maxN;
    private static int maxM;
    private static StringBuilder res = new StringBuilder();
    private static int[][] nums;
    public static void main(String[] args) {
        nums = new int[][]{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}};
        maxN = nums.length;
        maxM = nums[0].length;
        down(0,0);
        String str = res.toString();
        System.out.println(str.substring(0,str.length()-1));
    }


    // 向下读取
    public static void down(int i,int j){
        minM = j;
        if(maxN <= minN || i>= maxN) return;
        while(i < maxN){
            res.append(nums[i++][j]).append(" ");
        }
        right(maxN - 1,minM + 1);  // 输出的最后一行，下一列开始
    }

    // 向右读取
    public static void right(int i,int j){
        maxN = i;
        if(maxM <= minM || j >= maxM) return;
        while(j < maxM){
            res.append(nums[i][j++]).append(" ");
        }
        up(maxN - 1,maxM - 1);
    }

    // 向上读取
    public static void up(int i,int j){
        maxM = j;
        if(maxN <= minN || i <= minN) return;
        while(i > minN){
            res.append(nums[i--][j]).append(" ");
        }
        left(minN + 1,maxM - 1);

    }

    // 向左读取
    public static void left(int i,int j){
        minN = i;
        if(maxM <= minM || j <= minM) return;
        while(j > minM){
            res.append(nums[i][j--]).append(" ");
        }
        down(minN + 1,minM + 1);
    }

}
